<?php

namespace app\admin\server\order;

use app\admin\model\order\OrderMusicModel;
use app\admin\model\user\UserModel;
use app\admin\ServerBase;
use app\api\model\order\OrderMusicDetailApiModel;
use app\api\model\user\UserWriterWaterApiModel;
use app\logic\single\GoodsSingle;
use app\logic\traits\ExcelTraits;
use think\facade\Db;
use think\Request;

class OrderMusicServer extends ServerBase
{
    use ExcelTraits;
    protected $model;
    protected $request;
    /**
     * @var mixed
     */
    protected $param;
    /**
     * @var UserWriterWaterApiModel
     */
    private $userWriterWaterModel;

    function __construct( Request $request)
    {
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new OrderMusicModel();
        $this->userWriterWaterModel = new UserWriterWaterApiModel();
    }

    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        [$list,$count] = $this->model->getList($this->request,[],'*',$page,$limit);
        $result['data']=$list;
        $result['count']=$count;
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }

    function getDetail(){
        $id =  $this->request->param('id');
        $info = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        $order_detail = OrderMusicDetailApiModel::getByOrderId($id);
        $buyer = UserModel::getInfoById($info['buyer_id']);
        $this->getMessageCode('adminMessage.success.select',2);
        return ['info'=>$info,'detail'=>$order_detail,'buyer'=>$buyer];
    }
    function export(){
        [$list,$count] = $this->model->getList($this->request,[],'*',1,999999);
        $header=[
            '编号',
            '订单号',
            '金额',
            '买家昵称',
            '买家手机号',
            '状态',
            '支付方式',
            '创建时间',
            '支付时间',
        ];
        $buyer_ids = array_column($list,'buyer_id');
        $buyers = UserModel::getInfosByIds($buyer_ids,'nickname,mobile');
        $data=[];
        $status_arr = $this->model::getStatusArr();
        $pay_arr = $this->model::getPayArr();
        foreach ($list as $val){
            $data[]=[
                $val['id'],
                $val['order_no'],
                $val['order_price'],
                $buyers[$val['buyer_id']]['nickname']??'',
                $buyers[$val['buyer_id']]['mobile']??'',
                $status_arr[$val['status']]??'',
                $pay_arr[$val['pay_type']]??'',
                $val['create_time'],
                $val['pay_time'],
            ];
        }
        $this->excelExport('音乐订单',$header,$data);

    }

    function audit(){
        $order_id = $this->request->param('order_id')??0;
        $order_detail_id = $this->request->param('order_detail_id')??0;
        if(empty($order_id) && empty($order_detail_id)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        if(!empty($order_id)){
            $order = $this->model->getFindById($order_id);
            if(empty($order)){
                $this->getMessageCode('adminMessage.error.select',1);
                return false;
            }
            $order_detail = OrderMusicDetailApiModel::where('writer_can_see',-1)->where('order_id',$order_id)->select();
        }else{
            $order_detail = OrderMusicDetailApiModel::where('id',$order_detail_id)->select();
        }
        if(empty($order_detail)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        Db::startTrans();
        try {
            foreach ($order_detail as &$val){
                if($val['writer_can_see'] == -1){
                    //修改状态
                    OrderMusicDetailApiModel::where('id',$val['id'])
                        ->update(['writer_can_see'=>1]);
                    $inc_goods_sale[]=[
                        'goods_id'=>$val['goods_id'],
                        'type'=>$val['type'],
                    ];
                    $writer_water_insert[]=[
                        'user_id'=>$val['sell_id'],
                        'type' => 2,
                        'price'=>$val['writer_price'],
                        'source'=>1,
                        'order_id'=>$order_id,
                        'order_detail_id'=>$val['id'],
                        'remark'=>'出售获得佣金'.$val['writer_price'] . '元',
                        'music_type'=>$val['type'],
                    ];
                }
            }
            if(!empty($inc_goods_sale)){
                GoodsSingle::getInstance()->IncSales($inc_goods_sale);
            }
            if(!empty($writer_water_insert)){
                $this->userWriterWaterModel->saveAll($writer_water_insert);
            }
            Db::commit();
            return true;
        }catch (\Exception $exception){
            Db::rollback();
            $this->ERROR=$exception->getMessage();
            return false;
        }
    }
}